#include<iostream>
#include<queue>

using namespace std;

#define int long long
int n;

signed main()
{
    priority_queue<int,vector<int> ,greater<int>>heap;
    cin >> n;
    for(int i = 1;i <= n;i ++)
    {
        int x; cin >> x;
        heap.push(x);
    }
    int ret = 0;
    while(heap.size() > 1)
    {
        int a = heap.top(); heap.pop();
        int b = heap.top(); heap.pop();
        ret += a + b;
        heap.push(a + b);
    }
    cout << ret << endl;
    return 0;
}